import { AxiosRequestConfig } from "axios";
import { instance } from "./instance";
class Abstract {
  headers = {
    ContentType: "application/json;charset=UTF-8",
  };
  getDefaultConfig() {
    return instance;
  }
  apiAxios({
    baseURL = this.getDefaultConfig().defaults.baseURL || "",
    ...params
  }: AxiosRequestConfig) {
    return new Promise((resolve, reject) => {
      instance({
        baseURL,
        ...params,
      } as AxiosRequestConfig)
        .then((res) => {
          resolve(res.data);
        })
        .catch((err) => {
          reject(err.message);
        });
    });
  }

  /**
   * GET类型的网络请求
   */
  getReq(config: AxiosRequestConfig) {
    return this.apiAxios({
      method: "GET",
      ...config,
    });
  }

  /**
   * POST类型的网络请求
   */
  postReq(config: AxiosRequestConfig) {
    return this.apiAxios({
      method: "POST",
      ...config,
    });
  }

  /**
   * PUT类型的网络请求
   */
  putReq({
    baseURL,
    headers,
    url,
    data,
    params,
    responseType,
  }: AxiosRequestConfig) {
    return this.apiAxios({
      baseURL,
      headers,
      method: "PUT",
      url,
      data,
      params,
      responseType,
    });
  }

  /**
   * DELETE类型的网络请求
   */
  deleteReq({
    baseURL,
    headers,
    url,
    data,
    params,
    responseType,
  }: AxiosRequestConfig) {
    return this.apiAxios({
      baseURL,
      headers,
      method: "DELETE",
      url,
      data,
      params,
      responseType,
    });
  }
}

export { Abstract };
